package top.wardyan.wdev.dbdevassist.action.user;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.TableItem;

import top.wardyan.base.util.DateUtil;
import top.wardyan.conf.Const;
import top.wardyan.control.WButton;
import top.wardyan.control.WComposite;
import top.wardyan.control.WLabel;
import top.wardyan.control.WTable;
import top.wardyan.control.WTableColumn;
import top.wardyan.control.WTableItem;
import top.wardyan.control.WText;
import top.wardyan.wdev.dbdevassist.action.base.BaseShell;
import top.wardyan.wdev.dbdevassist.action.base.WDialog;
import top.wardyan.wdev.dbdevassist.config.DDA;
import top.wardyan.wdev.dbdevassist.model.YTDic;
import top.wardyan.wdev.dbdevassist.model.YTUser;

public class UserShell2 extends BaseShell<UserShell2>{
	protected WComposite mainCom;
	protected WTable mainTable;
	protected WLabel mainInfo;
	protected WText mainSearchText;
	protected WButton btnMainAdd;
	
	public UserShell2() {
		super();
		this.createContent();
		this.setMaximized(true);
		this.resize();
	}
	
	private void resize() {
		mainCom.setBounds(10, topCom.getVertexY() + 10, this.getSize().x - 30, this.getSize().y - topCom.getVertexY() - 10);
		mainSearchText.setBounds(0, 0, mainCom.getSize().x - 70, 30);
		btnMainAdd.setBounds(mainSearchText.getVertexX() + 10, mainSearchText.getLocation().y, 60, mainSearchText.getSize().y);
		//mainTable.setBounds(0, 0, mainCom.getSize().x - 10, mainCom.getSize().y - 30);
		mainTable.setBounds(0, mainSearchText.getVertexY() + 10, mainCom.getSize().x - 10, mainCom.getSize().y - mainSearchText.getVertexY() - 40);
		
	}
	
	private void createContent() {
		addControlListener(new ControlAdapter() {
			@Override
			public void controlResized(ControlEvent e) {
				resize();
			}
		});
		
		mainCom = new WComposite(this, SWT.NONE, 10, topCom.getVertexY() + 10, this.getSize().x - 30, this.getSize().y - topCom.getVertexY() - 10, Const.COLOR_WHITE);
		
		mainSearchText = new WText(mainCom, 0, 0, mainCom.getSize().x - 70, 30, "", "");
		mainSearchText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				self.listMain(true);
			}
		});
		mainSearchText.setFocus();
		btnMainAdd = new WButton(mainCom, mainSearchText.getVertexX() + 10, mainSearchText.getLocation().y, 60, mainSearchText.getSize().y, "新增", new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				new WDialog(self, new UserAddShell()).open();
				listMain(true);
			}
		});
		;
		//mainTable = new WTable(mainCom, SWT.NONE);
		mainTable = new WTable(mainCom, 0, mainSearchText.getVertexY() + 10, mainCom.getSize().x - 10, mainCom.getSize().y - mainSearchText.getVertexY() - 40);
		//mainTable.setSize(1024, 720);
		mainTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				int index = mainTable.getSelectionIndex();
				if(index >= 0) {
					TableItem item = mainTable.getItem(index);
					if(item != null) {
						String sid = item.getText(0);
						new WDialog(self, new UserEditShell(Integer.valueOf(sid))).open();
						// refresh
						self.listMain(true);
					}
				}
			}
		});
		mainTable.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				//self.listCol();
			}
		});
					
		this.initColumn();
		
		this.listMain(true);
	}
	
	protected void initColumn() {
		WTableColumn col = new WTableColumn(mainTable, "sID", 100);
		col = new WTableColumn(mainTable, "账号", 100);
		col = new WTableColumn(mainTable, "密码", 60);
		col = new WTableColumn(mainTable, "名称", 100);
		col = new WTableColumn(mainTable, "选项", 100);
		col = new WTableColumn(mainTable, "计算机名", 150);
		col = new WTableColumn(mainTable, "IP", 150);
		col = new WTableColumn(mainTable, "备注", 200);
		col = new WTableColumn(mainTable, "状态", 100);
		col = new WTableColumn(mainTable, "创建时间", 160);
		col = new WTableColumn(mainTable, "最后修改", 160);
		col = new WTableColumn(mainTable, "版本", 60);
	}
	protected List<YTUser> users = null;
	protected void listMain(boolean force) {
		mainTable.removeAll();
		if(users == null || force) {
			users = DDA.getInstance().userService.list();
		}
		if(users != null) {
			String text = mainSearchText.getText();
			for(YTUser d : users) {
				if(text.length() == 0 || String.valueOf(d.getSID()).contains(text) || d.getXName().contains(text) || d.getXAccount().contains(text) 
						|| d.getXIP().contains(text) || d.getXNote().contains(text) || d.getXPCName().contains(text)) {
					WTableItem item = new WTableItem(mainTable, new String[] {
							String.valueOf(d.getSID()), d.getXAccount(), "", d.getXName(), String.valueOf(d.getXOption()), d.getXPCName(), d.getXIP(), 
							d.getXNote(), d.getSStatus().getXName(),DateUtil.format(d.getSCreateTime()), DateUtil.format(d.getSLastUpdate()), String.valueOf(d.getSVersion())
					});
				}
			}
		}
		
	}
	
	
	
	
	
	
	
	
	

	/** 暂时还是写死的, 以后要改成从数据库或者配置文件取 2020-02-10 21:33:13 **/
	@Override
	protected String title() {
		return "用户";
	}
	
	public static void main(String[] args) {
		new UserShell2().go();
	}

}
